//
//  StoreListViewController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/24.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "StoreListViewController.h"
#import "StoreListTableCell.h"

static NSString * const StoreListTableCellIde = @"StoreListTableCellIde";
@interface StoreListViewController ()

@end

@implementation StoreListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"店铺列表";
    
    [self setupPageHeader:YES footer:YES];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 60;
    [self.tableView registerNib:[UINib nibWithNibName:@"StoreListTableCell" bundle:nil] forCellReuseIdentifier:StoreListTableCellIde];
    [self headerRefresh];
}

- (void)requestPageData:(void (^)(YCNetworkingStatusCode, NSArray *))statusBlock {

    NSString *userId = [LBUserManage defaultManager].user_id;
    
    if (!userId)
    {
        
        [MBProgressHUD showMessageWithOutIcon:@"请登录后再次尝试"];
        return;
    }
    
    [LBNetworkingInstance requestGET:memberStoreList parameters:NSDictionaryOfVariableBindings(userId) showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:StoreDetailModel.class json:responseObject[@"Data"]];
            statusBlock(code, array);
            [self.tableView reloadData];
        }
        else
        {
            statusBlock(code, nil);
        }
        
    } failure:^(NSError * _Nonnull err) {
        
        statusBlock(YCNetworkingStatusCodeHTTPError, nil);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    StoreDetailModel *model = self.dataSource[indexPath.item];
    StoreListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:StoreListTableCellIde forIndexPath:indexPath];
    
    [cell.coverimage sd_setImageWithURL:[NSURL URLWithString:model.store_images.firstObject.image] placeholderImage:PLACE_HOLDER];
    cell.storeName.text = model.name;
    cell.introductionLabel.text = model.brief;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    StoreDetailModel *model = self.dataSource[indexPath.item];
    if (self.selectStoreBlock) self.selectStoreBlock(model);
    [self.navigationController popViewControllerAnimated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
